import com.cdancy.jenkins.rest.JenkinsClient;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.QueueItem;
import com.offbytwo.jenkins.model.QueueReference;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class JenkinsDemo {

    JenkinsClient jenkinsClient;
    JenkinsServer jenkinsServer;

    public JenkinsDemo() throws URISyntaxException {
        String server = "http://192.168.1.85:8080";
        String username = System.getenv("demouser");
        String password = System.getenv("demopassword");
        System.out.println("username=" + username);

        this.jenkinsClient = JenkinsClient.builder()
                .endPoint(server)
                .credentials(username + ":" + password)
                .build();

        this.jenkinsServer = new JenkinsServer(new URI(server), username, password);
    }

    private void demo() throws IOException, InterruptedException {
        String jobName = "test-llq";

        Map<String, String> params = new HashMap<>();
        params.put("BUILD_TARGET", "origin/master");
        QueueReference queueReference = this.jenkinsServer.getJob(jobName).build(params);

        QueueItem queueItem = this.jenkinsServer.getQueueItem(queueReference);
        System.out.println("queueItem.getId()=" + queueItem.getId());

        while (queueItem.getExecutable() == null) {
            Thread.sleep(1000);
            queueItem = this.jenkinsServer.getQueueItem(queueReference);
        }

        Long buildnumber = queueItem.getExecutable().getNumber();
        System.out.println("buildnumber=" + buildnumber);

        // 输出日志
        BuildWithDetails details = jenkinsServer.getJob(jobName).getBuildByNumber(buildnumber.intValue()).details();
        System.out.println(details.getConsoleOutputText());
    }

    private void canceldemo() throws IOException, InterruptedException {
        String jobName = "test-llq";

        Map<String, String> params = new HashMap<>();
        params.put("BUILD_TARGET", "origin/master");
        QueueReference queueReference = this.jenkinsServer.getJob(jobName).build(params);
        System.out.println("queueItem.getId()=" + this.jenkinsServer.getQueueItem(queueReference).getId());

        Thread.sleep(1000 * 10);

        QueueItem queueItem = this.jenkinsServer.getQueueItem(queueReference);
        System.out.println("queueItem.getId()=" + queueItem.getId());

        // 取消队列任务，或停止任务
        if (queueItem.getExecutable() == null) {
            // 可能还在排队
            System.out.println("cancel queueItem， id=" + queueItem.getId());
            this.jenkinsClient.api().queueApi().cancel(queueItem.getId().intValue());

            if(queueItem.getExecutable() != null){
                System.out.println("stop build, build number=" + queueItem.getExecutable().getNumber());
                jenkinsServer.getJob(jobName).getBuildByNumber(queueItem.getExecutable().getNumber().intValue()).Stop();
            }
        } else {
            System.out.println("stop build, build number=" + queueItem.getExecutable().getNumber());
            jenkinsServer.getJob(jobName).getBuildByNumber(queueItem.getExecutable().getNumber().intValue()).Stop();
        }

    }

    public static void main(String[] args) throws Exception {
//        new JenkinsDemo().demo();
        new JenkinsDemo().canceldemo();
    }


}
